﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace Gw2Api
{
    #region DeserializedObjects
    public class WvWMatches
    {
        public class Matchup
        {
            public string wvw_match_id;
            public string red_world_id;
            public string blue_world_id;
            public string green_world_id;

            public string ToString(WorldNameApi names)
            {
                return names.GetWorldFromId(green_world_id) + ", " + names.GetWorldFromId(blue_world_id) + ", " + names.GetWorldFromId(red_world_id);
            }
            public string GetRegion()
            {
                return ((WvWRegion)Convert.ToInt32(wvw_match_id.Split("-".ToCharArray()).First())).ToString();
            }
            public string GetRegionShort()
            {
                WvWRegion region = (WvWRegion)Convert.ToInt32(wvw_match_id.Split("-".ToCharArray()).First());
                switch (region)
                {
                    case WvWRegion.NorthAmerica:
                        return "NA";
                    case WvWRegion.Europe:
                        return "EU";
                    default:
                        return "?";
                }
            }
            public int GetTier()
            {
                return Convert.ToInt32(wvw_match_id.Split("-".ToCharArray()).Last());
            }
        }
        public List<Matchup> wvw_matches;
        public string GetMatchup(string id, WorldNameApi names)
        {
            Matchup mu = wvw_matches.Where(m => m.wvw_match_id == id).First();
            return mu.ToString(names);
        }
    }

    public class WvWObjectiveDiff : BaseDiff
    {
        public string objectiveId;
        public string matchupId;
        public WvWObjectiveDiff(string matchupId, string objectiveId, string from, string to)
        {
            this.objectiveId = objectiveId;
            this.matchupId = matchupId;
            this.from = from;
            this.to = to;
            Debug.WriteLine(this.ToString());
        }
        override public string ToString()
        {
            //return "[" + matchupId + "]At " + ts.ToLongTimeString() + ", " + WvWApi.GetObjectiveNameFromId(objectiveId) + " changed from " + from + " to " + to;
            return "[" + matchupId + "] At " + ts.ToLongTimeString() + ", " + objectiveId + " changed from " + from + " to " + to;
        }
    }

    /*public class WvWMatchDetails
    {
        public string id;
        public int[] scores;
        public class WvWMap
        {
            public string type;
            public int[] scores;
            public class Objective
            {
                public int id;
                public string owner;
                public string owner_guild;
                public string owner_guild_name;

                public string GuildName
                {
                    get
                    {
                        if (!string.IsNullOrEmpty(this.owner_guild) && string.IsNullOrEmpty(this.owner_guild_name))
                        {
                            GuildDetails guild = Gw2Api.GuildDetailsApi.GetGuildDetails(owner_guild);
                            this.owner_guild_name = guild.guild_name + " [" + guild.tag + "]";
                            return this.owner_guild_name;
                        }
                        else if (!string.IsNullOrEmpty(this.owner_guild_name))
                        {
                            return this.owner_guild_name;
                        }
                        else
                        {
                            return "";
                        }
                    }
                }
                public WvWFaction faction{
                    get
                    {
                        if (this.owner == "Red")
                        {
                            return WvWFaction.Red;
                        }
                        else if (this.owner == "Blue")
                        {
                            return WvWFaction.Blue;
                        }
                        else if (this.owner == "Green")
                        {
                            return WvWFaction.Green;
                        }
                        return WvWFaction.All;
                    }
                }
                public WvWObjectiveDiff Diff(string matchupId, Objective otherObjective)
                {
                    Debug.Assert(otherObjective != null || otherObjective.id == this.id);
                    if (otherObjective != null && otherObjective.owner != this.owner)
                    {
                        return new WvWObjectiveDiff(matchupId, this.id, otherObjective.owner, this.owner);
                    }
                    return null;
                }
            }
            public List<Objective> objectives;
            public List<WvWObjectiveDiff> Diff(string matchupId, WvWMap otherMap)
            {
                List<WvWObjectiveDiff> diffs = new List<WvWObjectiveDiff>();
                foreach (var obj in objectives)
                {
                    WvWObjectiveDiff diff = obj.Diff(matchupId, otherMap.objectives.FirstOrDefault(o => o.id == obj.id));
                    if (null != diff)
                    {
                        diffs.Add(diff);
                    }
                }
                return diffs;
            }
        }
        public List<WvWMap> maps;
        public int[] GetScores(WvWMaps map)
        {
            if (WvWMaps.All == map)
            {
                return scores;
            }
            else
            {
                return maps.Where(m => map.ToString() == m.type).First().scores;
            }
        }
        public int GetScore(WvWMaps map, WvWFaction faction)
        {
            return GetScores(map)[(int)faction];
        }
        public List<WvWObjectiveDiff> Diff(WvWMatchDetails otherDetail)
        {
            List<WvWObjectiveDiff> diffs = null;
            if (maps.Count == otherDetail.maps.Count)
            {
                diffs = new List<WvWObjectiveDiff>();
                foreach (var map in maps)
                {
                    List<WvWObjectiveDiff> mapDiff = map.Diff(this.id, otherDetail.maps.Where(m => m.type == map.type).First());
                    diffs = diffs.Concat(mapDiff).ToList();
                }
                
            }
            return diffs;
        }
    }*/

    public class WvWObjective
    {
        public int id;
        public string name;
        public int point;
        public WvWMaps map;
        public string tag;
        public WvWObjective(int id, string name, int point, WvWMaps map, string tag)
        {
            this.id = id;
            this.name = name;
            this.point = point;
            this.map = map;
            this.tag = tag;
        }
    }

    public enum WvWRegion
    {
        NorthAmerica = 1,
        Europe,
    }

    public enum WvWFaction
    {
        Red = 0,
        Blue,
        Green,
        Neutral,
    }

    public enum WvWMaps
    {
        Center = 1,
        RedHome,
        BlueHome,
        GreenHome,
        All,
        EdgeOfTheMists,
    }
    #endregion

    public class WvWApi : BaseApi
    {
        private WorldNameApi _worldNames;
        private ObjectiveApi _objectives;
        private WvWMatches _matches;
        private List<WvWMatch> _matchesV2;
        //private Dictionary<string, WvWMatchDetails> _details;
        private Dictionary<string, WvWMatch> _detailsV2;
        private UpgradeApi _upgrades; 
        private HashSet<string> _refreshList;
        private List<WvWObjectiveDiff> _changeHistory;
        private const int _historyLength = 5000;

        // The API currently doesn't have proper objective names, so we shall hard code this
        private static readonly IEnumerable<WvWObjective> _objectiveProperties = new[] {
            #region Objective Properties
            new WvWObjective(1, "Overlook", 25, WvWMaps.Center, "EB-R-Keep"),
            new WvWObjective(2, "Valley", 25, WvWMaps.Center, "EB-B-Keep"),
            new WvWObjective(3, "Lowlands", 25, WvWMaps.Center, "EB-G-Keep"),
            new WvWObjective(4, "Golanta Clearing", 5, WvWMaps.Center, "EB-B-1-Camp"),
            new WvWObjective(5, "Pangloss Rise", 5, WvWMaps.Center, "EB-R-2-Camp"),
            new WvWObjective(6, "Speldan Clearcut", 5, WvWMaps.Center, "EB-R-1-Camp"),
            new WvWObjective(7, "Danelon Passage", 5, WvWMaps.Center, "EB-G-2-Camp"),
            new WvWObjective(8, "Umberglade Woods", 5, WvWMaps.Center, "EB-G-1-Camp"),
            new WvWObjective(9, "Stonemist Castle", 35, WvWMaps.Center, "EB-C-Castle"),
            new WvWObjective(10, "Rogue’s Quarry", 5, WvWMaps.Center, "EB-B-2-Camp"),
            new WvWObjective(11, "Aldon’s Ledge", 10, WvWMaps.Center, "EB-G-3-Tower"),
            new WvWObjective(12, "Wildcreek Run", 10, WvWMaps.Center, "EB-G-4-Tower"),
            new WvWObjective(13, "Jerrifer’s Slough", 10, WvWMaps.Center, "EB-G-2-Tower"),
            new WvWObjective(14, "Klovan Gully", 10, WvWMaps.Center, "EB-G-1-Tower"),
            new WvWObjective(15, "Langor Gulch", 10, WvWMaps.Center, "EB-B-3-Tower"),
            new WvWObjective(16, "Quentin Lake", 10, WvWMaps.Center, "EB-B-4-Tower"),
            new WvWObjective(17, "Mendon’s Gap", 10, WvWMaps.Center, "EB-R-2-Tower"),
            new WvWObjective(18, "Anzalias Pass", 10, WvWMaps.Center, "EB-R-1-Tower"),
            new WvWObjective(19, "Ogrewatch Cut", 10, WvWMaps.Center, "EB-R-4-Tower"),
            new WvWObjective(20, "Veloka Slope", 10, WvWMaps.Center, "EB-R-3-Tower"),
            new WvWObjective(21, "Durios Gulch", 10, WvWMaps.Center, "EB-B-1-Tower"),
            new WvWObjective(22, "Bravost Escarpment", 10, WvWMaps.Center, "EB-B-2-Tower"),
            new WvWObjective(23, "Garrison", 25, WvWMaps.BlueHome, "BL-C-Keep"),
            new WvWObjective(24, "Champion’s Demense", 5, WvWMaps.BlueHome, "BL-S-Camp"),
            new WvWObjective(25, "Redbriar", 10, WvWMaps.BlueHome, "BL-SW-Tower"),
            new WvWObjective(26, "Greenlake", 10, WvWMaps.BlueHome, "BL-SE-Tower"),
            new WvWObjective(27, "Ascension Bay", 25, WvWMaps.BlueHome, "BL-W-Keep"),
            new WvWObjective(28, "Dawn’s Eyrie", 10, WvWMaps.BlueHome, "BL-NE-Tower"),
            new WvWObjective(29, "The Spiritholme", 5, WvWMaps.BlueHome, "BL-N-Camp"),
            new WvWObjective(30, "Woodhaven", 10, WvWMaps.BlueHome, "BL-NW-Tower"),
            new WvWObjective(31, "Askalion Hills", 25, WvWMaps.BlueHome, "BL-E-Keep"),
            new WvWObjective(32, "Etheron Hills", 25, WvWMaps.RedHome, "BL-E-Keep"),
            new WvWObjective(33, "Dreaming Bay", 25, WvWMaps.RedHome, "BL-W-Keep"),
            new WvWObjective(34, "Victor’s Lodge", 5, WvWMaps.RedHome, "BL-S-Camp"),
            new WvWObjective(35, "Greenbriar", 10, WvWMaps.RedHome, "BL-SW-Tower"),
            new WvWObjective(36, "Bluelake", 10, WvWMaps.RedHome, "BL-SE-Tower"),
            new WvWObjective(37, "Garrison", 25, WvWMaps.RedHome, "BL-C-Keep"),
            new WvWObjective(38, "Longview", 10, WvWMaps.RedHome, "BL-NW-Tower"),
            new WvWObjective(39, "The Godsword", 5, WvWMaps.RedHome, "BL-N-Camp"),
            new WvWObjective(40, "Cliffside", 10, WvWMaps.RedHome, "BL-NE-Tower"),
            new WvWObjective(41, "Shadaran Hills", 25, WvWMaps.GreenHome, "BL-E-Keep"),
            new WvWObjective(42, "Redlake", 10, WvWMaps.GreenHome, "BL-SE-Tower"),
            new WvWObjective(43, "Hero’s Lodge", 5, WvWMaps.GreenHome, "BL-S-Camp"),
            new WvWObjective(44, "Dreadfall Bay", 25, WvWMaps.GreenHome, "BL-W-Keep"),
            new WvWObjective(45, "Bluebriar", 10, WvWMaps.GreenHome, "BL-SW-Tower"),
            new WvWObjective(46, "Garrison", 25, WvWMaps.GreenHome, "BL-C-Keep"),
            new WvWObjective(47, "Sunnyhill", 10, WvWMaps.GreenHome, "BL-NW-Tower"),
            new WvWObjective(48, "Faithleap", 5, WvWMaps.GreenHome, "BL-NW-Camp"),
            new WvWObjective(49, "Bluevale Refuge", 5, WvWMaps.GreenHome, "BL-SW-Camp"),
            new WvWObjective(50, "Bluewater Lowlands", 5, WvWMaps.RedHome, "BL-SE-Camp"),
            new WvWObjective(51, "Astralholme", 5, WvWMaps.RedHome, "BL-NE-Camp"),
            new WvWObjective(52, "Arah’s Hope", 5, WvWMaps.RedHome, "BL-NW-Camp"),
            new WvWObjective(53, "Greenvale Refuge", 5, WvWMaps.RedHome, "BL-SW-Camp"),
            new WvWObjective(54, "Foghaven", 5, WvWMaps.GreenHome, "BL-NE-Camp"),
            new WvWObjective(55, "Redwater Lowlands", 5, WvWMaps.GreenHome, "BL-SE-Camp"),
            new WvWObjective(56, "The Titanpaw", 5, WvWMaps.GreenHome, "BL-N-Camp"),
            new WvWObjective(57, "Cragtop", 10, WvWMaps.GreenHome, "BL-NE-Tower"),
            new WvWObjective(58, "Godslore", 5, WvWMaps.BlueHome, "BL-NW-Camp"),
            new WvWObjective(59, "Redvale Refuge", 5, WvWMaps.BlueHome, "BL-SW-Camp"),
            new WvWObjective(60, "Stargrove", 5, WvWMaps.BlueHome, "BL-NE-Camp"),
            new WvWObjective(61, "Greenwater Lowlands", 5, WvWMaps.BlueHome, "BL-SE-Camp"),
            new WvWObjective(62, "Temple of Lost Prayers", 0, WvWMaps.RedHome, "BL-S-Ruin"),
            new WvWObjective(63, "Battle's Hollow", 0, WvWMaps.RedHome, "BL-SW-Ruin"),
            new WvWObjective(64, "Bauer's Estate", 0, WvWMaps.RedHome, "BL-NW-Ruin"),
            new WvWObjective(65, "Orchard Overlook", 0, WvWMaps.RedHome, "BL-NE-Ruin"),
            new WvWObjective(66, "Carver's Ascent", 0, WvWMaps.RedHome, "BL-SE-Ruin"),
            new WvWObjective(67, "Carver's Ascent", 0, WvWMaps.BlueHome, "BL-SE-Ruin"),
            new WvWObjective(68, "Orchard Overlook", 0, WvWMaps.BlueHome, "BL-NE-Ruin"),
            new WvWObjective(69, "Bauer's Estate", 0, WvWMaps.BlueHome, "BL-NW-Ruin"),
            new WvWObjective(70, "Battle's Hollow", 0, WvWMaps.BlueHome, "BL-SW-Ruin"),
            new WvWObjective(71, "Temple of Lost Prayers", 0, WvWMaps.BlueHome, "BL-S-Ruin"),
            new WvWObjective(72, "Carver's Ascent", 0, WvWMaps.GreenHome, "BL-SE-Ruin"),
            new WvWObjective(73, "Orchard Overlook", 0, WvWMaps.GreenHome, "BL-NE-Ruin"),
            new WvWObjective(74, "Bauer's Estate", 0, WvWMaps.GreenHome, "BL-NW-Ruin"),
            new WvWObjective(75, "Battle's Hollow", 0, WvWMaps.GreenHome, "BL-SW-Ruin"),
            new WvWObjective(76, "Temple of Lost Prayers", 0, WvWMaps.GreenHome, "BL-S-Ruin")
            #endregion 
        };
        public static int WvWObjectiveRuinsIdBounds = 62;
        public static WvWObjective GetObjectivePropertyByTag(WvWMaps map, string tag)
        {
            if (null == tag)
                return null;
            return _objectiveProperties.Where(o => o.map == map && o.tag == tag).FirstOrDefault();
        }
        public static WvWObjective GetObjectivePropertyById(int id)
        {
            return _objectiveProperties.Where(obj => obj.id == id).FirstOrDefault();
        }
        
        public Objective GetObjectivePropertyByTagV2(string matchup, WvWMaps map, string tag)
        {
            if (null == tag)
                return null;
            WvWMatch.WvWMatchMap wvWMatchMap = _detailsV2[matchup].maps.FirstOrDefault(m => m.type == map);
            if (wvWMatchMap == null)
                return null;
            int map_id = wvWMatchMap.id;
            return _objectives.objectives.FirstOrDefault(o => o.map_id == map_id && o.tag == tag);
        }
        public Objective GetObjectivePropertyById(string id)
        {
            return _objectives.objectives.FirstOrDefault(obj => obj.id == id);
        }

        public WvWApi()
        {
            Debug.WriteLine("Initializing new API instantce");
            //_details = new Dictionary<string, WvWMatchDetails>();
            _detailsV2 = new Dictionary<string, WvWMatch>();
            _refreshList = new HashSet<string>();
            _changeHistory = new List<WvWObjectiveDiff>();
            LoadMatches();
            LoadMatchesAsync();
            LoadMatchesV2Async();
        }

        public void AddMatchupToRefreshList(WvWRegion region, int tier)
        {
            string id = GetMatchupId(region, tier);
            AddMatchupToRefreshList(id);
        }

        public void AddMatchupToRefreshList(string id)
        {
            lock (_lock)
            {
                _refreshList.Add(id);
            }
        }

        public void RemoveMatchupFromRefreshList(WvWRegion region, int tier)
        {
            string id = GetMatchupId(region, tier);
            RemoveMatchupFromRefreshList(id);
        }

        public void RemoveMatchupFromRefreshList(string id)
        {
            lock (_lock)
            {
                _refreshList.Remove(id);
            }
        }

        public void ClearRefreshList()
        {
            lock (_lock)
            {
                _refreshList.Clear();
            }
        }

        override public void Refresh()
        {
            HashSet<string> refreshList = new HashSet<string>(_refreshList);
            foreach (var id in refreshList)
            {
                RefreshDetails(id);
            }
        }

        public void RefreshDetails(string id)
        {
            WvWMatch old = null;
            
            if (_detailsV2.ContainsKey(id))
            {
                old = _detailsV2[id];
            }
            LoadMatchDetails(id);
            if (null != old)
            {
                List<WvWObjectiveDiff> diff = _detailsV2[id].Diff(old);
                if (diff != null)
                {
                    _changeHistory = _changeHistory.Concat(diff).ToList();
                    if (_changeHistory.Count > _historyLength)
                    {
                        _changeHistory.RemoveRange(0, _changeHistory.Count - _historyLength);
                    }
                    OnUpdate(diff.ToList<BaseDiff>());
                }
                else
                {
                    _changeHistory.Clear();
                }
            }
        }

        public Gw2Api.WvWMatch.WvWMatchMap.WvWMatchObjective GetLiveObjectiveById(string matchup, string id)
        {
            lock (_lock)
            {
                if (_detailsV2.Keys.Contains(matchup))
                {
                    foreach (var m in _detailsV2[matchup].maps)
                    {
                        var ret = m.objectives.Where(o => o.id == id);
                        if (ret.Count() > 0)
                        {
                            return ret.First();
                        }
                    }
                }
            }
            return null;
        }

        public WvWFaction GetObjectiveOwner(string matchup, string id)
        {
            lock (_lock)
            {
                foreach (var m in _detailsV2[matchup].maps)
                {
                    foreach (var o in m.objectives)
                    {
                        if (o.id == id)
                        {
                            return o.faction;
                        }
                    }
                }
            }
            return WvWFaction.Neutral;
        }

        public List<WvWObjectiveDiff> GetAllChangesBetweenTimespans(TimeSpan tsFrom, TimeSpan tsTo)
        {
            lock (_lock)
            {
                return _changeHistory.Where(diff => DateTime.UtcNow - diff.ts < tsTo && DateTime.UtcNow - diff.ts > tsFrom && GetObjectivePropertyById(diff.objectiveId).type != Objective.ObjectiveType.Ruins).ToList();
            }
        }

        public List<WvWObjectiveDiff> GetAllChangesInLast5Mins()
        {
            lock (_lock)
            {
                return _changeHistory.Where(diff => DateTime.UtcNow - diff.ts < new TimeSpan(0, 5, 0) && GetObjectivePropertyById(diff.objectiveId).type != Objective.ObjectiveType.Ruins).ToList();
            }
        }

        public List<WvWObjectiveDiff> GetCampChangesInLast5Mins()
        {
            lock (_lock)
            {
                return _changeHistory.Where(diff => DateTime.UtcNow - diff.ts < new TimeSpan(0, 5, 0)
                                                    && GetObjectivePropertyById(diff.objectiveId).type == Objective.ObjectiveType.Camp).ToList();
            }
        }

        public List<WvWObjectiveDiff> GetOtherChangesInLast5Mins()
        {
            lock (_lock)
            {
                return _changeHistory.Where(diff => DateTime.UtcNow - diff.ts < new TimeSpan(0, 5, 0)
                                                    && GetObjectivePropertyById(diff.objectiveId).type != Objective.ObjectiveType.Camp).ToList();
            }
        }

        public WvWMatch GetMatchupByWorldNameV2(string name)
        {
            string worldid = _worldNames.GetWorldIdFromName(name);
            int id = int.Parse(worldid);
            WvWMatch match =_matchesV2.Where(m => m.all_worlds[WvWMatch.WvWTeam.red].Contains(id) ||
                                                           m.all_worlds[WvWMatch.WvWTeam.blue].Contains(id) ||
                                                           m.all_worlds[WvWMatch.WvWTeam.green].Contains(id)).FirstOrDefault();
            return match;
        }

        public List<WvWMatches.Matchup> GetMatchups()
        {
            return _matches.wvw_matches;
        }

        public List<WvWMatch> GetMatchupsV2()
        {
            return _matchesV2;
        }

        public List<WvWUpgrade> GetUpgrades()
        {
            return _upgrades.upgrades;
        }

        public string GetMatchup(string id)
        {
            LoadMatchDetails(id);
            //return _matches.GetMatchup(id, _worldNames) + "\n"
            //    + _details[id].GetScore(WvWMaps.All, WvWFaction.Green) + ", "
            //    + _details[id].GetScore(WvWMaps.All, WvWFaction.Blue) + ", "
            //    + _details[id].GetScore(WvWMaps.All, WvWFaction.Red);
            return _matches.GetMatchup(id, _worldNames);
        }
        public string GetMatchup(WvWRegion region, int tier)
        {
            string id = GetMatchupId(region, tier);
            return GetMatchup(id);
        }

        private void LoadMatches()
        {
            string s = LoadFromUrl(ApiUrls.Matches);
            _matches = ConvertFromJson<WvWMatches>(s);
            string s2 = LoadFromUrl(ApiUrls.V2.MatchesOverview);
            _matchesV2 = ConvertFromJson<List<WvWMatch>>(s2);
        }
        private void LoadMatchesAsync()
        {
            LoadFromUrlAsync(ApiUrls.Matches, new LoadFinishedHandler(DoneLoadMatches));
        }
        private void LoadMatchesV2Async()
        {
            LoadFromUrlAsync(ApiUrls.V2.MatchesOverview, new LoadFinishedHandler(DoneLoadMatchesV2));
        }
        private void DoneLoadMatches(string s, object userdata)
        {
            _matches = ConvertFromJson<WvWMatches>(s);
            if (_matches.wvw_matches.Count == 0)
            {
                LoadMatchesAsync();
            }
            else
            {
                _worldNames = Gw2ApiFactory.WorldNameApi;
                _objectives = Gw2ApiFactory.ObjectiveApi;
                _upgrades = Gw2ApiFactory.UpgradeApi;
                OnReady();
            }
        }

        private void DoneLoadMatchesV2(string s, object userdata)
        {
            _matchesV2 = ConvertFromJson<List<WvWMatch>>(s);
            if (_matchesV2 == null || _matchesV2.Count == 0)
            {
                LoadMatchesV2Async();
            }
        }

        private void LoadMatchDetails(string id)
        {
            //string s = LoadFromUrl(ApiUrls.MatchDetails + "?match_id=" + id);
            //_details[id] = ConvertFromJson<WvWMatchDetails>(s);
            string s = LoadFromUrl(ApiUrls.V2.Matches + "/" + id);
            _detailsV2[id] = ConvertFromJson<WvWMatch>(s);
        }
        private void LoadMatchDetailsAsync(string id)
        {
            LoadFromUrlAsync(ApiUrls.V2.Matches + "/" + id, DoneLoadMatchDetailsAsync, id);
        }
        private void DoneLoadMatchDetailsAsync(string s, object userdata)
        {
            _detailsV2[(string)userdata] = ConvertFromJson<WvWMatch>(s);
        }

        private static string GetMatchupId(WvWRegion region, int tier)
        {
            return (int)region + "-" + tier;
        }

        public static string GetObjectiveNameFromId(int id)
        {
            return GetObjectivePropertyById(id).name;
        }

        public string GetObjectiveNameFromId(string id)
        {
            return GetObjectivePropertyById(id).name;
        }

        private void LoadMatchObjectives()
        {

        }

        #region debugcode
        public void load()
        {
            string s = @"{""match_id"":""1-8"",""scores"":[103036,
121462,
208554],""maps"":[{""type"":""RedHome"",""scores"":[68046,
8216,
18274],""objectives"":[{""id"":32,""owner"":""Green""},
{""id"":33,""owner"":""Green""},
{""id"":34,""owner"":""Blue""},
{""id"":35,""owner"":""Green""},
{""id"":36,""owner"":""Blue""},
{""id"":37,""owner"":""Red""},
{""id"":38,""owner"":""Red""},
{""id"":39,""owner"":""Red""},
{""id"":40,""owner"":""Red""},
{""id"":50,""owner"":""Red"",""owner_guild"":""3608B50D-AC0B-4AF7-AA16-34854AEB9B00""},
{""id"":51,""owner"":""Red""},
{""id"":52,""owner"":""Red""},
{""id"":53,""owner"":""Red""}]},
{""type"":""GreenHome"",""scores"":[5419,
8983,
79062],""objectives"":[{""id"":41,""owner"":""Green"",""owner_guild"":""1B3195E0-F8EB-4E08-9823-1D19552E303E""},
{""id"":42,""owner"":""Red"",""owner_guild"":""9E4CF13C-5214-4367-91DE-33BC9B212155""},
{""id"":43,""owner"":""Red""},
{""id"":44,""owner"":""Green""},
{""id"":45,""owner"":""Green""},
{""id"":46,""owner"":""Green"",""owner_guild"":""CCAC79C4-ED90-44C6-87AC-3B7CD8BD054A""},
{""id"":47,""owner"":""Green"",""owner_guild"":""A0209CCA-CDB2-49A3-89C8-699565211AAA""},
{""id"":48,""owner"":""Green""},
{""id"":49,""owner"":""Green""},
{""id"":54,""owner"":""Green""},
{""id"":55,""owner"":""Green""},
{""id"":56,""owner"":""Green""},
{""id"":57,""owner"":""Green""}]},
{""type"":""BlueHome"",""scores"":[3895,
67198,
22067],""objectives"":[{""id"":23,""owner"":""Blue"",""owner_guild"":""68AB6C87-00F1-4E38-83F9-8750DBE0DB0A""},
{""id"":24,""owner"":""Blue""},
{""id"":25,""owner"":""Blue""},
{""id"":26,""owner"":""Green""},
{""id"":27,""owner"":""Blue""},
{""id"":28,""owner"":""Blue"",""owner_guild"":""868545E6-D0CA-486C-9357-780E929F1E13""},
{""id"":29,""owner"":""Blue""},
{""id"":30,""owner"":""Blue"",""owner_guild"":""22153272-C85A-485E-A6CF-D5B099E65184""},
{""id"":31,""owner"":""Blue""},
{""id"":58,""owner"":""Blue""},
{""id"":59,""owner"":""Blue""},
{""id"":60,""owner"":""Blue""},
{""id"":61,""owner"":""Green""}]},
{""type"":""Center"",""scores"":[25676,
37065,
89151],""objectives"":[{""id"":1,""owner"":""Green""},
{""id"":2,""owner"":""Green"",""owner_guild"":""22FB9270-BF6C-44EA-8F71-B538AA3F49FB""},
{""id"":3,""owner"":""Green"",""owner_guild"":""F8824304-8C7A-4A7C-B4B0-7CB7F14B1B28""},
{""id"":4,""owner"":""Blue""},
{""id"":5,""owner"":""Green""},
{""id"":6,""owner"":""Green""},
{""id"":7,""owner"":""Green""},
{""id"":8,""owner"":""Green""},
{""id"":9,""owner"":""Green"",""owner_guild"":""CECEEEFD-5C94-4221-B948-C75E393E19FD""},
{""id"":10,""owner"":""Green""},
{""id"":11,""owner"":""Green"",""owner_guild"":""E2E2B9E3-829A-4F8B-AECA-DCF40FABA7BD""},
{""id"":12,""owner"":""Green""},
{""id"":13,""owner"":""Green""},
{""id"":14,""owner"":""Green""},
{""id"":15,""owner"":""Green""},
{""id"":16,""owner"":""Green""},
{""id"":17,""owner"":""Green""},
{""id"":18,""owner"":""Green""},
{""id"":19,""owner"":""Green""},
{""id"":20,""owner"":""Green""},
{""id"":21,""owner"":""Green""},
{""id"":22,""owner"":""Green""}]}]}";
            //_details["1-8"] = ConvertFromJson<WvWMatchDetails>(s);
        }
        #endregion
    }
}
